;
(function() {
  new Vue({
    el: '#contract-list',
    data: {
      permissions: JSON.parse(localStorage.getItem('permissions')), //权限
      dataList: [], // 合同列表数据
      keywords: '', // 信息搜索
      listMenu: [ // 审核状态
        {
          'name': '草稿',
          'code': 'CG'
        },
				{
				  'name': '审批中',
				  'code': 'WA'
				},
        {
          'name': '已生效',
          'code': 'EF'
        },
				{
				  'name': '审批拒绝',
				  'code': 'RJ'
				},
        {
          'name': '已作废',
          'code': 'ZF'
        }
      ],
      selectedListMenuCode: [],
      currentPage: 1, // 页面
      limit: ['10'], // 条数
      pageLimits: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      totalPage:0,
      totalCount:0,
      auditStateFlag: false, // 审核状态标记
      proId: '', // 项目ID
      isShowSubmitLayer: false,
      submitLayerData: {
        companyName: '',
        beginDate: '',
        endDate: '',
        id: ''
      },
      columns: '#', // 排序的列
      orders: '#', // 排序方式
      stOrder: '1', // 0 没有排序  1 生序   2 降序
    },
    filters: {
      formatDate: function(time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      auditState: function(item) {
        if (item === '全部') return '#'
        return hlCodeConvert.contractAuditState(item)
      },
      formatUndefined: function(item) {
        if (!item) return '--'
        return item
      },
      formatNum: function(item) {
        if (item === undefined) return '--'
        return hlNumberUtil.formatNum(item)
      }
    },
    methods: {
      selectMenuList(){
        this.currentPage = 1
        this.search()
      },
      // 查询合同
      search: function() {
        var self = this;
        var data = {
          keywords: self.keywords.trim() || '#',
          status: self.selectedListMenuCode.length ? self.selectedListMenuCode.join() : '#',
          page: self.currentPage,
          limit: self.limit[0],
          columns: self.columns,
          orders: self.orders,
        }

        hlhttp.get({
          url: 'tenant/contract_alter/_search',
          data: data,
          success: function(data) {
            self.dataList = data.alterationList;
            self.totalCount = data.totalCount;
            self.totalPage = data.totalPage;

            loginPrem.loginList()
          }
        })
      },

      // 按时间排序
      sortBySTTime: function() {
				this.columns = 'ED'
        $('.du-icon').removeClass('icon-Gm-sort-ascending').removeClass('icon-Gm-sort-descending').addClass('icon-Gm-sort-default')
        if (this.stOrder == '0') {
          this.stOrder = '1'
          this.orders = 'ASC'
          this.search()
          $('.bt-icon').removeClass('icon-Gm-sort-default').addClass('icon-Gm-sort-ascending')
          return false;
        }
        if (this.stOrder == '1') {
          this.stOrder = '2'
          this.orders = 'DESC'
          this.search()
          $('.bt-icon').removeClass('icon-Gm-sort-ascending').addClass('icon-Gm-sort-descending')
          return false;
        }
        if (this.stOrder == '2') {
          this.stOrder = '1'
          this.orders = 'ASC'
          $('.bt-icon').removeClass('icon-Gm-sort-descending').addClass('icon-Gm-sort-ascending')
          this.search()
          return false;
        }
      },

      // 重置
      reset: function() {
        this.currentPage = 1
        this.keywords = ''
        this.search()
      },
      resetTabbar(){
        this.selectedListMenuCode = [];
        this.currentPage = 1
        this.search()
      },
      // 初始化项目 默认加载所有的项目数据
      initProgrammer: function() {
        var self = this;
        getProduct(
          function(arr) {
            self.proId = arr[1].join(',') || '#'
            self.search()
          },
          function(arr) {
            self.proId = arr[0].join(',') || '#'
            self.search()
          },
          function(arr) {
            self.proId = arr[0].join(',') || '#'
            self.search()
          }
        )
      },
      // 分页
      updateLimit: function(arr) {
        this.limit = [arr[0].code];
        this.currentPage = 1;
        this.search()
      },
      // 作废合同
      invalidContract: function(id) {
        var self = this;
        hllayer.confirm1('此操作不可逆转，确定作废变更单吗？', function() {
          hlhttp.put({
            url: 'contract_alter/_cancel',
            data: { "alter_id": id },
            success: function(data) {
              self.search()
            }
          })
        })
      },
      // 提交合同
      submitContract: function(id) {
        var self = this;
        hllayer.confirm1('此操作不可逆转，变更内容将立即生效！', function() {
          hlhttp.put({
            url: 'contract_history/details_submit',
            data: {alter_id: id },
            success: function(data) {
              self.search()
            }
          })
        })
      }
    },
    mounted: function() {
      hlSelectUi(true)
      this.initProgrammer()
    }
  })

}())
